#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-08-17 1:48 下午
# @Author  : yangdy
# @File    : 2020-08-17.110.平衡二叉树
# @Software: PyCharmCE


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        if not root:
            return True
        al = self.isBalanced(root.left)
        ar = self.isBalanced(root.right)
        lh = root.left.h if root.left else 0
        rh = root.right.h if root.right else 0
        root.h = max(lh, rh) + 1
        if not al or not ar:
            return False
        if abs(lh-rh) > 1:
            return False
        return True
